package com.example.senior_bug.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.senior_bug.EntityClass.LogisticsInfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

//物流API调用工具类
public class ToolUtils {
    public static String post(String url, String body){
        String result = "";
        try {
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(50000);
            conn.setReadTimeout(200000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Tracking-Api-Key","13124af8-4a33-4baf-97f6-a03b0bd2af14");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    result += System.lineSeparator();
                }
                result += line;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<LogisticsInfo> getLogistics(String LogisticsNum, String LogisticsType) {

        String result = "";
        try {

            String LogisticsNum1 = LogisticsNum;
            String Logistics = LogisticsType;

            String body = "{\"tracking_number\":\""+LogisticsNum+"\",\"carrier_code\":\""+LogisticsType+"\"}";
            String result1 = ToolUtils.post("https://api.51tracking.com/v2/trackings/post", body);

            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL("https://api.51tracking.com/v2/trackings/" + LogisticsType + "/" + LogisticsNum + "/cn");
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoInput(true);
            conn.setConnectTimeout(50000);
            conn.setReadTimeout(200000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Tracking-Api-Key", "13124af8-4a33-4baf-97f6-a03b0bd2af14");
            // 提交数据
            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    result += System.lineSeparator();
                }
                result += line;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject object = JSON.parseObject(result);
        String data = object.getString("data");
        JSONObject object1 = JSON.parseObject(data);
        String origin_info = object1.getString("origin_info");
        JSONObject object2 = JSON.parseObject(origin_info);

        List<LogisticsInfo> logisticsInfos = JSON.parseArray(object2.getJSONArray("trackinfo").toJSONString(), LogisticsInfo.class);

        return logisticsInfos;
    }
}
